import java.util.ArrayList;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.concurrent.atomic.AtomicReference;

public class ThreadTest2 {

    public static void main(String[] args) throws InterruptedException {
        ExecutorService executorService = Executors.newFixedThreadPool(3);
        CountDownLatch countDownLatch = new CountDownLatch(3);
        AtomicReference<Boolean> resultHolder = new AtomicReference<>(null);

        System.out.println(Thread.currentThread().getName() + "--开始执行");

        for (int i = 0; i < 3; i++) {
            executorService.execute(() -> {
                try {

                    System.out.println(Thread.currentThread().getName() + "----");
                    boolean result = Math.random() < 0.5;
                    System.out.println(Thread.currentThread().getName() + "----" + result);
                    if (!result) {
                        resultHolder.set(false);

                    } else if (resultHolder.get() == null) {
                        resultHolder.compareAndSet(null, true);
                    }
                } finally {
                    countDownLatch.countDown();
                }
            });

        }

        System.out.println(Thread.currentThread().getName() + "--1");

        countDownLatch.await();
        System.out.println(Thread.currentThread().getName() + "--2");

        if (resultHolder.get() == null) {
            System.out.println("Error: Unexpected result.");
        } else if (resultHolder.get()) {
            System.out.println("allTrue");
        } else {
            System.out.println("false");
        }
        System.out.println(Thread.currentThread().getName() + "--3");


        executorService.shutdown();

    }
}
